package com.bedrock.project.inside.service.impl;

import com.bedrock.common.utils.DateUtils;
import com.bedrock.common.utils.IdWord;
import com.bedrock.project.enums.WorkHourStatEnum;
import com.bedrock.project.inside.entity.WorkHourStat;
import com.bedrock.project.inside.mapper.WorkHourMapper;
import com.bedrock.project.inside.mapper.WorkHourStatMapper;
import com.bedrock.project.inside.service.IWorkHourStatService;
import com.bedrock.project.inside.vo.request.WorkHourStatReqVo;
import com.bedrock.project.inside.vo.response.ProjectStatResVo;
import com.bedrock.project.inside.vo.response.SaleStatResVo;
import com.bedrock.project.inside.vo.response.UserStatResVo;
import com.bedrock.project.inside.vo.response.WorkHourStatResVo;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 工时统计Service业务层处理
 * 
 * @author toby.zhang
 * @date 2020-04-30
 */
@Service
public class WorkHourStatServiceImpl implements IWorkHourStatService
{
    @Autowired
    private WorkHourStatMapper workHourStatMapper;

    @Autowired
    private WorkHourMapper workHourMapper;

    /**
     * 查询工时统计
     * 
     * @param id 工时统计ID
     * @return 工时统计
     */
    @Override
    public WorkHourStat selectWorkHourStatById(Long id)
    {
        return workHourStatMapper.selectWorkHourStatById(id);
    }

    /**
     * 查询工时统计列表
     * 
     * @param workHourStat 工时统计
     * @return 工时统计
     */
    @Override
    public List<WorkHourStat> selectWorkHourStatList(WorkHourStat workHourStat)
    {
        return workHourStatMapper.selectWorkHourStatList(workHourStat);
    }

    /**
     * 新增工时统计
     * 
     * @param workHourStat 工时统计
     * @return 结果
     */
    @Override
    public int insertWorkHourStat(WorkHourStat workHourStat)
    {
        workHourStat.setCreateTime(DateUtils.getNowDate());
        return workHourStatMapper.insertWorkHourStat(workHourStat);
    }

    /**
     * 修改工时统计
     * 
     * @param workHourStat 工时统计
     * @return 结果
     */
    @Override
    public int updateWorkHourStat(WorkHourStat workHourStat)
    {
        return workHourStatMapper.updateWorkHourStat(workHourStat);
    }

    /**
     * 批量删除工时统计
     * 
     * @param ids 需要删除的工时统计ID
     * @return 结果
     */
    @Override
    public int deleteWorkHourStatByIds(Long[] ids)
    {
        return workHourStatMapper.deleteWorkHourStatByIds(ids);
    }

    /**
     * 删除工时统计信息
     * 
     * @param id 工时统计ID
     * @return 结果
     */
    @Override
    public int deleteWorkHourStatById(Long id)
    {
        return workHourStatMapper.deleteWorkHourStatById(id);
    }

    @Override
    public void workHourStatisticsByDay() {
        //1.获取当天的工时信息
        List<WorkHourStat> datas = workHourMapper.selectWorkHourListByDay();
        //2.批量插入工时统计表
        if(CollectionUtils.isNotEmpty(datas)){
            IdWord idWord = new IdWord(1, 1);
            for(WorkHourStat data : datas){
                data.setId(idWord.nextId());
                //工时确认开始和结束日期不为空并且工时日期在确认开始和结束之间,表示该工时记录已经确认
                if(data.getConfirmStartDate()!=null && data.getConfirmEndDate()!=null
                        && data.getConfirmStartDate().before(data.getWorkHourDate())
                        && data.getConfirmEndDate().after(data.getWorkHourDate())
                ){
                  data.setIsConfirm(1);
                }
            }
        }
        workHourStatMapper.insertBatchWorkHourStat(datas);
    }

    @Override
    public List<ProjectStatResVo> selectProjectStatList(WorkHourStatReqVo workHourStatReqVo) {
        return workHourStatMapper.selectProjectStatList(workHourStatReqVo);
    }

    @Override
    public List<WorkHourStatResVo> selectWorkHourStatDetail(WorkHourStatReqVo workHourStatReqVo) {
        return workHourStatMapper.selectWorkHourStatDetail(workHourStatReqVo);
    }

    @Override
    public Object selectWorkHourStatChart(WorkHourStatReqVo workHourStatReqVo) {
        Object obj = null;
        String searchType = workHourStatReqVo.getSearchType();
        if(WorkHourStatEnum.WORK_HOUR_STAT_PIECHART.getCode().equals(searchType)){ //饼图
            List<Map> result = new ArrayList<>();
            List<WorkHourStatResVo> list = workHourStatMapper.selectWorkHourStatPieChartList(workHourStatReqVo);
            for(WorkHourStatResVo data:list){
                Map<String,Object> temp = new HashMap<>();
                temp.put("name",data.getWorkTypeName());
                temp.put("value",data.getWorkHourTotal());
                result.add(temp);
            }
            obj = result;
        }else if(WorkHourStatEnum.WORK_HOUR_STAT_BROKENLINE.getCode().equals(searchType)){ //折线图
            List<List<?>> brokenLine = new ArrayList<>();
            //x轴时间数组
            List<String> dates = DateUtils.getBetweenDate(workHourStatReqVo.getStartTime(),workHourStatReqVo.getEndTime());
            brokenLine.add(dates);
            Map<String,WorkHourStatResVo> workHourStatMap = new HashMap<>();
            List<WorkHourStatResVo> list = workHourStatMapper.selectWorkHourStatBrokenLineList(workHourStatReqVo);
            for(WorkHourStatResVo data:list){
                workHourStatMap.put(DateUtils.dateTime(data.getWorkHourDate()),data);
            }
            //日期数组和工时统计记录不一致,需要造数据
            //y轴内部成本价数组
            List<Double> costInners = new ArrayList<>();
            //y轴外部成本价数组
            List<Double> costOutings = new ArrayList<>();
            WorkHourStatResVo temp = new WorkHourStatResVo();
            temp.setCostInner(0d);
            temp.setCostOuting(0d);
            for(String date: dates){
                if(workHourStatMap.get(date)==null){
                    workHourStatMap.put(date,temp);
                }
                WorkHourStatResVo data = workHourStatMap.get(date);
                costInners.add(data.getCostInner());
                costOutings.add(data.getCostOuting());
            }
            brokenLine.add(costInners);
            brokenLine.add(costOutings);
            obj = brokenLine;
        }
        return obj;
    }

    @Override
    public List<UserStatResVo> selectUserStatList(WorkHourStatReqVo workHourStatReqVo) {
        return workHourStatMapper.selectUserStatList(workHourStatReqVo);
    }

    @Override
    public List<SaleStatResVo> selectSaleStatList(WorkHourStatReqVo workHourStatReqVo) {
        return workHourStatMapper.selectSaleStatList(workHourStatReqVo);
    }

}
